package com.siashan.toolkit.oss.core;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.AccessControlException;
import java.util.Properties;

/**
 * 配置文件读取类
 *
 * @author  siashan
 * @since  1.0.1
 **/
@Slf4j
public class OssConfiguration {
    private static Properties defaultProperty;
    private static final String UNIOSS_4_J_PROPERTIES = "unioss4j.properties";


    /**
     * 构造方法
     */
    private OssConfiguration() {
        init();
    }

    private static final class ConfigureHolder {
        private static final OssConfiguration CONFIGURATION = new OssConfiguration();
    }

    public static final OssConfiguration getInstance() {
        return ConfigureHolder.CONFIGURATION;
    }


    /**
     * 初始化
     */
    static void init() {
        //初始化默认配置
        defaultProperty = new Properties();
        //读取自定义配置
        boolean loaded = loadProperties(defaultProperty, "." + File.separatorChar + UNIOSS_4_J_PROPERTIES)
                || loadProperties(defaultProperty, OssConfiguration.class.getClassLoader().getResourceAsStream(UNIOSS_4_J_PROPERTIES));
        if (!loaded && log.isDebugEnabled()) {
            log.debug("unioss4j:没有加载到unioss4j.properties属性文件!");
        }
    }



    /**
     * 加载属性文件
     *
     * @param props 属性文件实例
     * @param path 属性文件路径
     * @return 是否加载成功
     */
    protected static boolean loadProperties(Properties props, String path) {
        try {
            File file = new File(path);
            if (file.exists() && file.isFile()) {
                props.load(new FileInputStream(file));
                return true;
            }
        } catch (IOException ignore) {
            //异常忽略
            ignore.printStackTrace();
        }
        return false;
    }

    /**
     * 加载属性文件
     *
     * @param props 属性文件实例
     * @param is 属性文件流
     * @return 是否加载成功
     */
    private static boolean loadProperties(Properties props, InputStream is) {
        try {
            if (is != null) {
                props.load(is);
                return true;
            }
        } catch (IOException ignore) {
            //异常忽略
            ignore.printStackTrace();
        }
        return false;
    }

    /**
     * 获取属性值
     *
     * @param name 属性名称
     * @return 属性值
     */
    public  String getProperty(String name) {
        return getProperty(name, null);
    }

    /**
     * 获取属性值
     *
     * @param name 属性名
     * @param fallbackValue 默认返回值
     * @return 属性值
     */
    public  String getProperty(String name, String fallbackValue) {
        String value;
        try {
            //从全局系统获取
            value = System.getProperty(name, null);
            if (null == value) {
                //先从系统配置文件获取
                value = defaultProperty.getProperty(name, fallbackValue);
            }
        } catch (AccessControlException ace) {
            // Unsigned applet cannot access System properties
            value = fallbackValue;
        }
        return value;
    }
}
